package com.hxwb.robot.util;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hxwb.robot.httpEntity.PlateSortItem;
import com.hxwb.robot.request.OrderReq;
import com.hxwb.robot.response.LastPriceRes;
import com.hxwb.robot.response.TradePlateRes;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpUtil {


    public static PlateSortItem findTradePlate(String symbol) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("symbol", symbol);
        String result = HttpRequest.post(PathUtil.getPlateItemUrl)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();
        PlateSortItem plateSortItem = new PlateSortItem();
        if (!StringUtils.isEmpty(result) && isJson(result)) {
            TradePlateRes tradePlateRes = JSONArray.parseObject(result, TradePlateRes.class);
            if (!CollectionUtils.isEmpty(tradePlateRes.getAsk()) && !CollectionUtils.isEmpty(tradePlateRes.getBid())) {
                plateSortItem.setBuyOne(tradePlateRes.getBid().get(0).getPrice());
                plateSortItem.setSellOne(tradePlateRes.getAsk().get(0).getPrice());
            }
        }else{
            return null;
        }
        System.out.println(plateSortItem);

        return plateSortItem;
    }
    public static BigDecimal getLastPrice(String symbol) {
        BigDecimal lastPrice = null;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("symbol", symbol);
        paramMap.put("size",1);
        String result = HttpRequest.post(PathUtil.getLastPriceUrl)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();
        System.out.println("最新市价结果:"+result);
        if (!StringUtils.isEmpty(result)) {
            List<LastPriceRes> lastPriceResList = JSONArray.parseArray(result,LastPriceRes.class);
            System.out.println("市价列表"+lastPriceResList);
            lastPrice= lastPriceResList.get(0).getPrice();
        }
        System.out.println("最新市价"+lastPrice);

        return lastPrice;
    }

    public static String post(Map<String, Object> paramMap, String url) {
        String result = HttpRequest.post(url)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();
        if (isJson(result)) {
            return result;
        }
        System.out.println(new Date()+"机器人获取了错误的token");
        return "error";
    }

    public static String postAddOrder(OrderReq orderReq, String url, String token) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("amount", orderReq.getAmount());
        paramMap.put("symbol", orderReq.getSymbol());
        paramMap.put("type", orderReq.getType());
        paramMap.put("price", orderReq.getPrice());
        paramMap.put("direction", orderReq.getDirection());
        String result = HttpRequest.post(url)
                    .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8")//头信息，多个头信息多次调用此方法即可
                    .header("x-auth-token", token)
                    .form(paramMap)
                    .timeout(20000)//超时，毫秒
                    .execute().body();
            System.out.println("订单" + result);
            if (isJson(result)) {
                return result;
            }

        return new Date() +"机器人下单失败！";
    }

    public static boolean isJson(String content) {
        try {
            JSONObject.parseObject(content);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
